package com.cema.manage.project.manage.antiCheatDetail.controller;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.cema.manage.common.utils.StringUtils;
import com.cema.manage.project.manage.antiCheatDetail.domain.AntiCheatDetail;
import com.cema.manage.project.manage.antiCheatDetail.domain.AntiCheatDetailVo;
import com.cema.manage.project.manage.antiCheatDetail.service.IAntiCheatDetailService;
import com.cema.manage.project.manage.users.domain.Users;
import com.cema.manage.project.manage.users.service.IUsersService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.framework.web.controller.BaseController;
import com.cema.manage.framework.web.domain.Message;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cema.manage.framework.web.page.TableDataInfo;

import javax.annotation.Resource;


/**
 * 封号记录 信息操作处理
 *
 * @author reasahi
 * @date 2024-07-15
 */
@Controller
@RequestMapping("/manage/antiCheatDetail")
public class AntiCheatDetailController extends BaseController {
    private String prefix = "manage/antiCheatDetail";

    @Autowired
    private IAntiCheatDetailService antiCheatDetailService;

    @Resource
    private IUsersService usersService;

    @GetMapping()
    @RequiresPermissions("manage:antiCheatDetail:view")
    public String antiCheatDetail() {
        return prefix + "/antiCheatDetail";
    }

    /**
     * 查询封号记录列表
     */
    @RequiresPermissions("manage:antiCheatDetail:list")
    @ResponseBody
    @GetMapping("/list")
    public TableDataInfo list(AntiCheatDetailVo antiCheatDetailVo) {
        startPage();
        EntityWrapper entityWrapper = new EntityWrapper();
        String value = antiCheatDetailVo.getSearchValue();
        if (StrUtil.isNotEmpty(value)) {
            entityWrapper.eq("us_id", antiCheatDetailVo.getSearchValue());
        }
        List<AntiCheatDetail> list = antiCheatDetailService.selectList(entityWrapper);
        return getDataTable(list);
    }

    /**
     * 新增封号记录
     */
    @RequiresPermissions("manage:antiCheatDetail:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增封号记录
     */
    @RequiresPermissions("manage:antiCheatDetail:add")
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("userId", id);
        return prefix + "/add";
    }

    /**
     * 修改封号记录
     */
    @RequiresPermissions("manage:antiCheatDetail:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model) {
        AntiCheatDetail antiCheatDetail = antiCheatDetailService.selectById(id);
        model.addAttribute("antiCheatDetail", antiCheatDetail);
        return prefix + "/edit";
    }

    /**
     * 保存封号记录
     */
    @RequiresPermissions("manage:antiCheatDetail:save")
    @PostMapping("/save")
    @ResponseBody
    public Message save(AntiCheatDetail antiCheatDetail) {
        Integer id = antiCheatDetail.getId();
        Users users = usersService.selectById(antiCheatDetail.getUsId());
        if (ObjectUtil.isNull(users)) {
            return Message.error("封号用户不存在");
        }
        if (StringUtils.isNotNull(id)) {
            if (antiCheatDetailService.updateById(antiCheatDetail)) {
                return Message.success();
            }
            return Message.error();
        } else {
            //每个用户只能添加一次
            EntityWrapper<AntiCheatDetail> entityWrapper = new EntityWrapper<>();
            AntiCheatDetail antiCheatDetailEntity = new AntiCheatDetail();
            antiCheatDetailEntity.setUsId(antiCheatDetail.getUsId());
            entityWrapper.setEntity(antiCheatDetailEntity);
            int count = antiCheatDetailService.selectCount(entityWrapper);
            if (count > 0) {
                return Message.error("同一个用户不能重复封号");
            }
            if (antiCheatDetailService.insert(antiCheatDetail)) {
                users.setStatus(2);
                usersService.setStatus(users);
                return Message.success();
            }
            return Message.error();
        }
    }

    /**
     * 删除封号记录
     */
    @RequiresPermissions("manage:antiCheatDetail:remove")
    @PostMapping("/remove/{id}")
    @ResponseBody
    public Message remove(@PathVariable("id") Integer id) {
        AntiCheatDetail antiCheatDetail = antiCheatDetailService.selectById(id);
        if (ObjectUtil.isNull(antiCheatDetail)) {
            return Message.error("封号记录不存在后者已经删除");
        }
        Users users = usersService.selectById(antiCheatDetail.getUsId());
        if (ObjectUtil.isNull(users)) {
            return Message.error("封号用户不存在");
        }
        if (antiCheatDetailService.deleteById(id)) {
            users.setStatus(1);
            usersService.setStatus(users);
            return Message.success();
        }
        return Message.error();
    }

    /**
     * 批量删除封号记录
     */
    @RequiresPermissions("manage:antiCheatDetail:batchRemove")
    @PostMapping("/batchRemove")
    @ResponseBody
    public Message remove(@RequestParam("ids[]") Integer[] ids) {
        List list = new ArrayList();
        for (Integer n : ids) {
            list.add(n);
        }
        if (antiCheatDetailService.deleteBatchIds(list)) {
            return Message.success();
        }
        return Message.error();
    }

}
